package p.ithorns.integration.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

//@SpringBootApplication
public class ZkApplication {

    static CountDownLatch cdl = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        //SpringApplication.run(ZkApplication.class, args);

        ZooKeeper zk = new ZooKeeper("192.168.2.111:2181,192.168.2.112:2181,192.168.2.113:2181,192.168.2.114:2181", 3000,
                new Watcher() {
                    @Override
                    public void process(WatchedEvent event) {
                        Event.KeeperState state = event.getState();
                        Event.EventType type = event.getType();
                        String path = event.getPath();

                        System.out.println(String.format("default watcher state:[%s], type:[%s], path:[%s]", state.name(), type, path));
                        switch (state) {
                            case Expired:
                                break;
                            case AuthFailed:
                                break;
                            case Disconnected:
                                break;
                            case SyncConnected:
                                break;
                            case ConnectedReadOnly:
                                break;
                            case SaslAuthenticated:
                                break;
                            case NoSyncConnected:
                                break;
                            case Unknown:
                                break;
                            default:
                                break;
                        }
                    }
                });

        ZooKeeper.States state = zk.getState();
        System.out.println("zk state: " + state.name());
        switch (state) {
            case CONNECTING:
                break;
            case CONNECTED:
                break;
            case NOT_CONNECTED:
                break;
            case CONNECTEDREADONLY:
                break;
            case ASSOCIATING:
                break;
            case AUTH_FAILED:
                break;
            case CLOSED:
                break;
            default: break;
        }

        //String path = zk.create("/xxoo", "hello".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

        zk.getData("/xxoo", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("getdata watcher: " + watchedEvent.toString());
            }
        }, new AsyncCallback.DataCallback() {
            // 获取到结果后回调
            @Override
            public void processResult(int rc, String path, Object ctx, byte data[], Stat stat) {
                System.out.println(String.format("rc: %d, path: %s, ctx: %s, stat: %s", rc, path, ctx.toString(), stat.toString()));
                System.out.println("get data: " + new String(data));
                //cdl.countDown();
            }
        }, "abc");

        zk.setData("/xxoo", "newdata".getBytes(), -1);
        cdl.await();
    }
}
